<?php
    require('init.php');
    require('header.php');
    $employe = Employe::rechercheEmploye(Tools::getValue('id', 'GET'));
    $civilites = Civilite::getCivilites();
    if(Tools::getValue('ajout')) {
        $employe = new Employe(0, Civilite::rechercheCivilite(Tools::getValue('id_civilite')), Profil::rechercheProfil(2), 
                            Tools::getValue('mdp'), Tools::getValue('nom'), Tools::getValue('prenom'), 
                            Tools::getValue('telephone'), Tools::getValue('mobile'), Tools::getValue('email'), 
                            Tools::getValue('date_naissance'), new DateTime(), new DateTime(), Tools::getValue('valide'));
        $employe->add();
    }
    else if(Tools::getValue('modif')) {
        $employe->setCivilite(Civilite::rechercheCivilite(Tools::getValue('id_civilite')));
        $employe->setNom(Tools::getValue('nom'));
        $employe->setPrenom(Tools::getValue('prenom'));
        $employe->setTelephone(Tools::getValue('telephone'));
        $employe->setMobile(Tools::getValue('mobile'));
        $employe->setEmail(Tools::getValue('email'));
        $employe->setDateNaissance(Tools::getValue('date_naissance'));
        $employe->setValide(Tools::getValue('valide'));
        $employe->update();
    }
    
    if(!$employe instanceof Employe) {
        echo    '<form method="POST" action="details_employe.php">
                    <div class="container_12 formulaire">
                        <h2>
                            Ajout d\'un employé
                        </h2>
                        <div class="grid_1">
                            &nbsp;
                        </div>
                        <div class="grid_3 label_admin">
                            Civilité<br>
                            Nom : <br>
                            Prénom : <br>
                            Téléphone<br>
                            Mobile<br>
                            Email<br>
                            Mot de passe<br> 
                            Date de naissance<br>
                            Valide
                        </div>
                        <div class="grid_6 formulaire_admin">
                            <select name="id_civilite">';
        foreach($civilites as $civilite) {
            echo                '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
        }
        echo                '</select>
                            <input type="text" name="nom" /><br>
                            <input type="text" name="prenom" /><br>
                            <input type="text" name="telephone" /><br>
                            <input type="text" name="mobile" /><br>
                            <input type="text" name="email" /><br>
                            <input type="password" name="mdp" /><br>
                            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" /><br>
                            <input type="checkbox" name="valide" value="1" CHECKED /><br>
                            <input type="submit" class="btn" value="Ajouter" name="ajout" />
                        </div>
                    </div>
                </form>';
    }
    else {
        $employe_checked = "";
        if($employe->getValide() == 1) {
            $employe_checked = "CHECKED";
        }
        echo    '<form method="POST" action="details_employe.php?id='.$employe->getId().'">
                    <div class="container_12 formulaire">
                        <h2>
                            Modification du compte employé de '.$employe.'
                        </h2>
                        <div class="grid_1">
                            &nbsp;
                        </div>
                        <div class="grid_3 label_admin">
                            Civilité<br>
                            Prénom : <br>
                            Nom : <br>
                            Téléphone<br>
                            Mobile<br>
                            Email<br>
                            Date de naissance<br>
                            Valide
                        </div>
                        <div class="grid_6 formulaire_admin">
                            <select name="id_civilite">
                                <option value="'.$employe->getCivilite()->getId().'">'.$employe->getCivilite().'</option>';
        foreach($civilites as $civilite) {
            echo                '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
        }
        echo                '</select>
                            <input type="text" name="prenom" value="'.$employe->getPrenom().'"/><br>
                            <input type="text" name="nom" value="'.$employe->getNom().'"/><br>
                            <input type="text" name="telephone" value="'.$employe->getTelephone().'"/><br>
                            <input type="text" name="mobile" value="'.$employe->getMobile().'"/><br>
                            <input type="text" name="email" value="'.$employe->getEmail().'"/><br>
                            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" value="'.$employe->getDateNaissance('d/m/Y').'"/><br>
                            <input type="checkbox" name="valide" value="1" '.$employe_checked.'/><br>
                            <input type="submit" value="Modifier" name="modif" />
                        </div>
                    </div>
                </form>';
    }
?>
